//
//  PageGuideManager.m
//  LeaveLight
//
//  Created by 张 on 2019/3/12.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "PageGuideManager.h"

@interface PageGuideManager ()

@property (nonatomic, strong) NSArray * imgArr;
@property (nonatomic, strong) UIImageView * imgView;
@property (nonatomic, assign) NSInteger tapIndex;

@end

@implementation PageGuideManager

DEF_SINGLETON(PageGuideManager)

- (void)showGuideImgArr:(NSArray *)imgArr
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:imgArr.firstObject] == NO) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:imgArr.firstObject];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.imgArr = [NSArray arrayWithArray:imgArr];
        self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self.imgView setBackgroundColor:RGBAlpha(0, 0, 0, 0.3)];
        [self.imgView setContentMode:UIViewContentModeScaleAspectFill];
        self.imgView.userInteractionEnabled = YES;
        [self.imgView setImage:[UIImage imageNamed:imgArr.firstObject]];
        self.tapIndex = 0;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTap:)];
        [self.imgView addGestureRecognizer:tap];
        [[UIApplication sharedApplication].keyWindow addSubview:self.imgView];
    }
}

- (void)hideGuideView:(UIView *)imgView
{
    [UIView animateWithDuration:1 animations:^{
        imgView.alpha = 1;
    } completion:^(BOOL finished) {
        imgView.alpha = 0;
        [imgView removeFromSuperview];
    }];
}

- (void)imgTap:(UITapGestureRecognizer *)gesture
{
    self.tapIndex ++;
    if (self.imgArr.count == 1) {
        [self hideGuideView:self.imgView];
    }else
    {
        if (self.tapIndex == self.imgArr.count) {
            CGPoint selectPoint = [gesture locationInView:gesture.view];
            NSLog(@"%@",[NSValue valueWithCGPoint:selectPoint]);
            if (selectPoint.y >= gesture.view.center.y + 120) {
                //再看一遍
                self.tapIndex = 0;
                [self.imgView setImage:[UIImage imageNamed:self.imgArr[0]]];
            }else{
               [self hideGuideView:gesture.view];
            }
        }else{
            [self.imgView setImage:[UIImage imageNamed:self.imgArr[self.tapIndex]]];
        }
    }
}










@end
